/*
 * readconfig.c
 *
 *  Created on: 15/apr/2014
 *      Author: kappa
 */

#include "config.h"



/*Ritorna un puntatore alla struttura dati
 * contenente la configurazione, se non e'
 * possibile leggere la configurazione ritorna null*/

void getConfiguration(CNF_PTR config){
	int isnewfile = 0;
	char * configstring = NULL;
	void* filehandle = NULL;
	
#ifdef _WIN32
	HANDLE filehandle = INVALID_HANDLE_VALUE;
	if(!SetCurrentDirectory(CONFDIR)){
		logMessage(INFO_LEV,"directory di configurazione non trovata");
		logMessage(INFO_LEV,"creazione directory di configurazione... ");
		createDir(CONFDIR);
		logMessage(INFO_LEV,"... OK");
		SetCurrentDirectory(CONFDIR);
	}
	filehandle = (HANDLE) openOrCreateFile(CONFFILE, WRITE, &isnewfile);
#else
	filehandle = openOrCreateFile(CONFFILE, WRITE, &isnewfile);
#endif

	if(isnewfile){
		getDefaultConfig(&config);
		writeDefaultConfig(filehandle, config);
		closeFile(filehandle);

	}
	else{
		configstring = readAllFile(filehandle);
		closeFile(filehandle);
		free(filehandle);
		filehandle = NULL;

		if(configstring == NULL){
			notifyError(ERR_CFG_READ,1,1,CONFFILE);
		}
	
		else {
			decodeConfigString(&config,configstring);
			free(configstring);
			configstring = NULL;
		}
	}
	if(filehandle != NULL){
		free(filehandle);
		filehandle = NULL;
	}
}


void getDefaultConfig(CNF_PTR* config){

	config[0]->isMultiThreaded = IS_MULTI_THREAD_DEFAULT;
	config[0]->pop3Port = POP3_PORT_DEFAULT;
	config[0]->smtpPort = SMTP_PORT_DEFAULT;
	config[0]->mailboxPath = MAILBOX_PATH_DEFAULT;
	config[0]->maxConcurrentPT = MAX_CONC_DEFAULT;
	config[0]->maxMessageLength = MAX_MSG_LEN_DEFAULT;
	config[0]->usersConfPath = USR_CONF_PATH_DEFAULT;
	config[0]->secretsFile = SECRETS_PATH_DEFAULT;
}
/*
 * Scrive il file di configurazione. Riceve il file descriptor come parametro
 * e non ritorna nulla....
 * */

void writeDefaultConfig(void* pfd, CNF_PTR config){
	char * fileContent = prepareConfBuffer(config);
	writeFile(pfd, fileContent);
	free(fileContent);
	fileContent = NULL;
}




char * prepareConfBuffer(CNF_PTR config){

	char *configStr;
	char numConversion[25];


	configStr = (char *)getMemory(512,CHAR_TYPE);
	memset(configStr,'\0',512);

#ifdef _WIN32
	strcat(configStr, STR_ISMULTH);
		strcat(configStr, ":");
		memset(numConversion, '\0',25);
		sprintf(numConversion, "%d", config->isMultiThreaded);
		strcat(configStr, numConversion);
		strcat(configStr,"\r");

		strcat(configStr, STR_MXCONC);
		strcat(configStr, ":");
		memset(numConversion, '\0',25);
		sprintf(numConversion, "%d", config->maxConcurrentPT);
		strcat(configStr, numConversion);
		strcat(configStr,"\r");

		strcat(configStr, STR_MXCONC);
		strcat(configStr, ":");
		memset(numConversion, '\0',25);
		sprintf(numConversion, "%d", config->maxConcurrentPT);
		strcat(configStr, numConversion);
		strcat(configStr,"\r");

		strcat(configStr, STR_MXMSGLEN);
		strcat(configStr, ":");
		memset(numConversion, '\0',25);
		sprintf(numConversion, "%d", config->maxMessageLength);
		strcat(configStr, numConversion);
		strcat(configStr,"\r");

		strcat(configStr, STR_SMTP_PORT);
		strcat(configStr, ":");
		memset(numConversion, '\0',25);
		sprintf(numConversion, "%d", config->smtpPort);
		strcat(configStr, numConversion);
		strcat(configStr,"\r");

		strcat(configStr, STR_POP3_PORT);
		strcat(configStr, ":");
		memset(numConversion, '\0',25);
		sprintf(numConversion, "%d", config->pop3Port);
		strcat(configStr, numConversion);
		strcat(configStr,"\r");

		strcat(configStr, STR_USR_CONF_PATH);
		strcat(configStr, ":");
		strcat(configStr, config->usersConfPath);
		strcat(configStr,"\r");

		strcat(configStr, STR_MAILBOX_PATH);
		strcat(configStr, ":");
		strcat(configStr, config->mailboxPath);
		strcat(configStr,"\r");

		strcat(configStr, STR_SECRETS_PATH);
		strcat(configStr, ":");
		strcat(configStr, config->secretsFile);
		strcat(configStr,"\r");

#else

	strcat(configStr, STR_ISMULTH);
	strcat(configStr, ":");
	memset(numConversion, '\0',25);
	sprintf(numConversion, "%d", config->isMultiThreaded);
	strcat(configStr, numConversion);
	strcat(configStr,"\n");

	strcat(configStr, STR_MXCONC);
	strcat(configStr, ":");
	memset(numConversion, '\0',25);
	sprintf(numConversion, "%d", config->maxConcurrentPT);
	strcat(configStr, numConversion);
	strcat(configStr,"\n");

	strcat(configStr, STR_MXCONC);
	strcat(configStr, ":");
	memset(numConversion, '\0',25);
	sprintf(numConversion, "%d", config->maxConcurrentPT);
	strcat(configStr, numConversion);
	strcat(configStr,"\n");

	strcat(configStr, STR_MXMSGLEN);
	strcat(configStr, ":");
	memset(numConversion, '\0',25);
	sprintf(numConversion, "%d", config->maxMessageLength);
	strcat(configStr, numConversion);
	strcat(configStr,"\n");

	strcat(configStr, STR_SMTP_PORT);
	strcat(configStr, ":");
	memset(numConversion, '\0',25);
	sprintf(numConversion, "%d", config->smtpPort);
	strcat(configStr, numConversion);
	strcat(configStr,"\n");

	strcat(configStr, STR_POP3_PORT);
	strcat(configStr, ":");
	memset(numConversion, '\0',25);
	sprintf(numConversion, "%d", config->pop3Port);
	strcat(configStr, numConversion);
	strcat(configStr,"\n");

	strcat(configStr, STR_USR_CONF_PATH);
	strcat(configStr, ":");
	strcat(configStr, config->usersConfPath);
	strcat(configStr,"\n");

	strcat(configStr, STR_MAILBOX_PATH);
	strcat(configStr, ":");
	strcat(configStr, config->mailboxPath);
	strcat(configStr,"\n");
	
	strcat(configStr, STR_SECRETS_PATH);
	strcat(configStr, ":");
	strcat(configStr, config->secretsFile);
	strcat(configStr,"\n");
#endif

	return configStr;
}

void decodeConfigString(CNF_PTR* cnfpntr,char* configstring){
	
	char property[64] = {0};
	char * stringvalue = NULL;
	int i = 0, j = 0, value = -1, endline = 0, indexStartValue = -1, configSize = -1;


	if(configstring!=NULL){
		configSize = strlen(configstring);
		i=0;
		while(i < configSize){
			// configstring[i]!= '\0'){
			j=0;
			memset(property,'\0',64);
			while(configstring[i]!=':' && configstring[i]!='\0'){
				property[j++] = configstring[i++];
			}

			i++;
			indexStartValue =  i;
			endline = 0;

			while(i < configSize && configstring[i]!='\n' && configstring[i]!='\r' && configstring[i]!='\0'){
				i++;
				endline++;
			}
			if(i == configSize){
				break;
			}
			else i++;
//			else{
//
//				notifyError(ERR_CFG_READ,1,1,CONFFILE);
//			}

			stringvalue = (char *) getMemory(endline+1, CHAR_TYPE);
			memset(stringvalue,'\0',endline+1);
			strncpy(stringvalue, &configstring[indexStartValue], endline);
			if(isdigit(stringvalue[0])!=0){
				value = atoi(stringvalue);
				memset(stringvalue,'\0', endline+1);
			}

			setProperty(&cnfpntr, property, value, stringvalue);
			free(stringvalue);
			stringvalue = NULL;
			value =-1;
		}
	}
	else{
		notifyError(ERR_CFG_READ,1,1,CONFFILE);
	}
}

void setProperty(CNF_PTR** cnfptr, char *property, int value, char *stringvalue){

	if(strcmp(property, STR_ISMULTH)==0){
		cnfptr[0][0]->isMultiThreaded = value;
	}
	else if(strcmp(property, STR_MXCONC)==0){
		cnfptr[0][0]->maxConcurrentPT = value;
	}
	else if(strcmp(property, STR_MXMSGLEN)==0){
		cnfptr[0][0]->maxMessageLength = value;
	}
	else if(strcmp(property, STR_SMTP_PORT)==0){
		cnfptr[0][0]->smtpPort = value;
	}
	else if(strcmp(property, STR_POP3_PORT)==0){
		cnfptr[0][0]->pop3Port = value;
	}
	else if(strcmp(property, STR_USR_CONF_PATH)==0){
		cnfptr[0][0]->usersConfPath = (char *)getMemory(50, CHAR_TYPE);
//		#ifdef _WIN32
//			strcpy(cnfptr[0][0]->usersConfPath,stringvalue);
//		#else
		strcpy(cnfptr[0][0]->usersConfPath,stringvalue);
//		#endif
	}
	else if(strcmp(property, STR_MAILBOX_PATH)==0){
		cnfptr[0][0]->mailboxPath = (char *)getMemory(50, CHAR_TYPE);
//		#ifdef _WIN32
//			strcpy(cnfptr[0][0]->mailboxPath, stringvalue);
//		#else
		strcpy(cnfptr[0][0]->mailboxPath,stringvalue);
//		#endif
	}
	else if(strcmp(property, STR_SECRETS_PATH)==0){
		cnfptr[0][0]->secretsFile = (char *)getMemory(50, CHAR_TYPE);
//		#ifdef _WIN32
//			strcpy(cnfptr[0][0]->secretsFile, stringvalue);
//		#else
		strcpy(cnfptr[0][0]->secretsFile,stringvalue);
//		#endif
	}
}

/*Log su stdout il log delle informazioni di configurazione*/
void printConfiguration(CNF_PTR cnfpntr){

	char conf[512]={'\0'};
	char *appendString = NULL;

	if(isNull(cnfpntr) == 0){
		appendString = "\nisMultiThreaded: %d\n";
		memset(conf,'\0',512);
		
		strcat(conf, appendString);
		appendString = "maxConcurrentPT: %d\n";
		strcat(conf, appendString);
		appendString = "maxMessageLength: %d\n";
		strcat(conf, appendString);
		appendString = "pop3Port: %d\n";
		strcat(conf, appendString);
		appendString = "smtpPort: %d\n";
		strcat(conf, appendString);
		appendString = "usersConfPath: %s \n";
		strcat(conf, appendString);
		appendString = "mailboxPath: %s \n";
		strcat(conf, appendString);
		appendString = "secretsFile: %s \n";
		strcat(conf, appendString);

		printf(conf,
				cnfpntr->isMultiThreaded, cnfpntr->maxConcurrentPT,
				cnfpntr->maxMessageLength, cnfpntr->pop3Port, cnfpntr->smtpPort,
				cnfpntr->usersConfPath == NULL ? "": cnfpntr->usersConfPath,
				cnfpntr->mailboxPath == NULL ? "": cnfpntr->mailboxPath,
				cnfpntr->secretsFile == NULL ? "": cnfpntr->secretsFile);
	}
	else{
		printf("\nERRORE: Impossibile stampare i valori di configurazione.");
	}
}

void initConfigObject(CNF_PTR config){

	config->isMultiThreaded = 0;
	config->maxConcurrentPT = 0;
	config->maxMessageLength = 0;
	config->smtpPort = 0;
	config->pop3Port = 0;
	config->usersConfPath = NULL;
	config->mailboxPath = NULL;
	config->secretsFile = NULL;
}
